﻿using System;
using System.Web.Security;
using System.Web.UI.WebControls;
using YouthCMS.Data;

namespace YouthCMS
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            try
            {
                if (Session["Username"] != null)
                {

                    if (HyperLinkLogOut != null) HyperLinkLogOut.Visible = true;
                    lblShowSessionName.Text = string.Format("Velkommen: <p class='links'>{0}</p><br/>Du kan logge ud ", Session["Username"].ToString());
                    LoginView1.FindControl("Login1").Visible = false;


                }
                else
                {
                    hLinkStudent.Visible = false;
                    HyperLink4.Visible = false;
                    HyperLink3.Visible = false;
                    HyperLink2.Visible = false;
                    HyperLinkAdmin.Visible = false;
                }
            }
            catch (Exception ex)
            {
                Response.Redirect(string.Format("~/errors/PageNotFound.aspx?error={0}", ex.Message));
            }


        }

        protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
        {
            var data = new Database();
            var lgn1 = LoginView1.FindControl("Login1");

            var txtPassword = (TextBox)lgn1.FindControl("Password");
            var tmpTextPass = txtPassword.Text;

            var txtUsername = (TextBox)lgn1.FindControl("Username");
            var tmpTextUser = txtUsername.Text;

            string userInputtedHashedPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpTextPass, "sha1");
            if (userInputtedHashedPassword != null)
            {
                var lowerInvariant = userInputtedHashedPassword.ToLowerInvariant();
                bool authenticated = false;
                authenticated = data.UserLogin(tmpTextUser, lowerInvariant);
                e.Authenticated = authenticated;
                if (authenticated == true)
                {
                    Session["Username"] = tmpTextUser;
                    Response.Redirect("Default.aspx?user=" + Session["Username"]);

                }
            }
        }

        protected string ReturnElementId()
        {
            var lgn1 = LoginView1.FindControl("Login1");

            var txtLoginButtonID = (TextBox)lgn1.FindControl("LoginButton");
            var tmpTextLogin = txtLoginButtonID.ClientID.ToString();

            return tmpTextLogin;
        }

        /*
        protected void Login1_LoggedIn(object sender, EventArgs e)
        {
            // if there is no returnUrl in the query string , we redirect based on user role
            if (string.IsNullOrEmpty(Request.QueryString["ReturnUrl"]))
            {
               
                // please don't use User.IsInRole here , because it will not be populated yet at this stage.
                if (Roles.IsUserInRole(Login1.UserName, "Admins"))
                    Response.Redirect("~/Admins/Default.aspx");
                else if (Roles.IsUserInRole(Login1.UserName, "Editors"))
                    Response.Redirect("~/Default.aspx");
            }
        }*/

    }
}
